/*
	DIMGA
	Distributed Island Model Genetic Algorithm 

	Copyright (c) 2006 Nikola Sivacki nsivacki@gmail.com

	This program is free software; you can redistribute it and/or
	modify in under the terms of the GNU General Public Licence as
	published by the Free Software Foundation; either version 2 of
	the Licence, or [at your option] any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAT PURPOSE. See the
	GNU General Public Licence for more details.

	You should have received a copy of the GNU General Public Licence
	along with this program; if not, write to the Free Software 
	Foundation Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
// Evaluator.h
#ifndef EVALUATOR_H
#define EVALUATOR_H

//#include "genotype.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#include "csv.h"

class Genotype;

class Evaluator
{
public:
	Evaluator(){}

	virtual float Evaluate(unsigned char* _g, long _iSize)
  {
    float sum=0.0f;

    int i;
    for(i = 0; i < traindata->getsize(); i++)
    {
      int ninput=traindata->getlength(0);

      float* input = traindata->getbuffer(i) + (last ? 0 : 1);
      float out = evaluate_single(input, _g, _iSize);
      float *target = last ? input + ninput: input;
      float delta = fabs(target[0] - out);
      sum += (delta*delta);
    }

    return (sum/i);
  }

	virtual float Test(unsigned char* _g, long _iSize) { return 0.0f;}

	virtual void print(unsigned char* _g, long _iSize) { return; }

	virtual void toFile(unsigned char* _g, long _lSize, const char* file)
  {
    FILE *f = NULL;
    f = fopen(file, "w");

    if (NULL == f)
    {
        perror("Cannot create file");
    }

    long lWritten = fwrite(_g, 1, _lSize, f);
    if (lWritten != _lSize)
    {
      perror("Error writing genome");
		}

    fclose(f);
	}

  virtual float evaluate_single(float* input, unsigned char* _model, long _iSize) = 0;

  // TODO: this needs to be called from configure(), it is currently breaking Evaluate() above
  void assign_training_set(csvfile* _csv, bool _ends_with_target = true)
  { 
    traindata=_csv;
    last=_ends_with_target;
  }


protected:
  csvfile* traindata;
  bool last;
};


#endif
